package com.ruoyi.appraise.controller;

import com.ruoyi.appraise.domain.TechnicianAppraise;
import com.ruoyi.appraise.service.ITechnicianAppraiseService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.mapper.TechnicianItemOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 技师评价Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/technicianAppraise/technicianAppraise")
public class TechnicianAppraiseController extends BaseController {
    @Autowired
    private ITechnicianAppraiseService technicianAppraiseService;
    @Resource
    private TechnicianItemOrderMapper technicianItemOrderMapper;

    /**
     * 查询技师评价列表
     */
    @PreAuthorize("@ss.hasPermi('technicianAppraise:technicianAppraise:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnicianAppraise technicianAppraise) {
        if (!CheckEmptyUtil.isEmpty(technicianAppraise.getTechnicianName())){
            List<Long> tIds = technicianItemOrderMapper.seleTid(technicianAppraise.getTechnicianName());
            if (!CheckEmptyUtil.isEmpty(tIds)){
                technicianAppraise.setTechnicianIds(tIds);
            }else {
                return getDataTable(Collections.emptyList());
            }
        }else {
            technicianAppraise.setTechnicianIds(Collections.emptyList());
        }
        startPage();
        List<TechnicianAppraise> list = technicianAppraiseService.selectTechnicianAppraiseList(technicianAppraise);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianAppraise order : list) {
                String userName = technicianItemOrderMapper.selectUserName(order.getUserId());
                order.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                String tName = technicianItemOrderMapper.selectTName(order.getTechnicianId());
                order.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出技师评价列表
     */
    @PreAuthorize("@ss.hasPermi('technicianAppraise:technicianAppraise:export')")
    @Log(title = "技师评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnicianAppraise technicianAppraise) {
        if (!CheckEmptyUtil.isEmpty(technicianAppraise.getTechnicianName())){
            List<Long> tIds = technicianItemOrderMapper.seleTid(technicianAppraise.getTechnicianName());
            if (!CheckEmptyUtil.isEmpty(tIds)){
                technicianAppraise.setTechnicianIds(tIds);
            }else {
                technicianAppraise.setTechnicianIds(Collections.emptyList());
            }
        }else {
            technicianAppraise.setTechnicianIds(Collections.emptyList());
        }
        List<TechnicianAppraise> list = technicianAppraiseService.selectTechnicianAppraiseList(technicianAppraise);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianAppraise order : list) {
                String userName = technicianItemOrderMapper.selectUserName(order.getUserId());
                order.setUserName(CheckEmptyUtil.isEmpty(userName) ? "" : userName);
                String tName = technicianItemOrderMapper.selectTName(order.getTechnicianId());
                order.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        ExcelUtil<TechnicianAppraise> util = new ExcelUtil<TechnicianAppraise>(TechnicianAppraise.class);
        util.exportExcel(response, list, "技师评价数据");
    }

    /**
     * 获取技师评价详细信息
     */
    @PreAuthorize("@ss.hasPermi('technicianAppraise:technicianAppraise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(technicianAppraiseService.selectTechnicianAppraiseById(id));
    }

    /**
     * 新增技师评价
     */
    @PreAuthorize("@ss.hasPermi('technicianAppraise:technicianAppraise:add')")
    @Log(title = "技师评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnicianAppraise technicianAppraise) {
        return toAjax(technicianAppraiseService.insertTechnicianAppraise(technicianAppraise));
    }

    /**
     * 修改技师评价
     */
    @PreAuthorize("@ss.hasPermi('technicianAppraise:technicianAppraise:edit')")
    @Log(title = "技师评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnicianAppraise technicianAppraise) {
        return toAjax(technicianAppraiseService.updateTechnicianAppraise(technicianAppraise));
    }

    /**
     * 删除技师评价
     */
    @PreAuthorize("@ss.hasPermi('technicianAppraise:technicianAppraise:remove')")
    @Log(title = "技师评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(technicianAppraiseService.deleteTechnicianAppraiseByIds(ids));
    }
}
